package com.example.shushiyunsst.fragment.main.home.child.miner.sub

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.global.AppGlobals
import com.example.shushiyunsst.R
import com.google.android.material.tabs.TabLayout

/**
 * 星树交易(买)
 * */
class StartTransferBuyingFragment : BaseFragmentEx(), StartTransferLab {

    override val title = R.string.start_transfer_buying_lab

    companion object {
        fun newInstance(): StartTransferBuyingFragment {
            val args = Bundle()
            val fragment = StartTransferBuyingFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private lateinit var mTabLayout: TabLayout

    private lateinit var mViewPager: ViewPager

    private val subPages = arrayOf<BaseFragmentEx>(
        StartTransferBuyingSubFragment.newInstance("V-I型"),
        StartTransferBuyingSubFragment.newInstance("V-II型"),
        StartTransferBuyingSubFragment.newInstance("V-III型"),
        StartTransferBuyingSubFragment.newInstance("V-Ⅳ型"),
        StartTransferBuyingSubFragment.newInstance("V-Ⅴ型")
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport=false
    }

    override fun getLayoutId() = R.layout.start_transfer_buying_fragment

    @SuppressLint("ClickableViewAccessibility")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mTabLayout = view.findViewById(R.id.mTabLayout)
        mViewPager = view.findViewById(R.id.mViewPager)
        mViewPager.adapter = object
            :
            FragmentStatePagerAdapter(childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount() = subPages.size

            override fun getItem(position: Int) = subPages[position]

            override fun getPageTitle(position: Int): CharSequence? {
                val lab = getItem(position) as StartTransferBuyingLab
                return lab.title
            }
        }
        mTabLayout.setupWithViewPager(mViewPager)
        mViewPager.addOnPageChangeListener(mPageChangeListener)


    }

    override fun onDestroy() {
        super.onDestroy()
        mViewPager.removeOnPageChangeListener(mPageChangeListener)
    }

    private val mPageChangeListener = object :
        ViewPager.OnPageChangeListener {
        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {

        }

        override fun onPageSelected(position: Int) {
            val lastPageIndex = (mViewPager.adapter?.count ?: 1) - 1
            if (lastPageIndex == 0) return

            val currentItem = mViewPager.currentItem

            if (currentItem <= 0 || currentItem >= lastPageIndex - 1) {
                rootView.parent.requestDisallowInterceptTouchEvent(false)
            } else {
                rootView.parent.requestDisallowInterceptTouchEvent(true)
            }
        }

        override fun onPageScrollStateChanged(state: Int) {

        }
    }
}